import BaseSprite from "../../../Script/Base/BaseSprite";
import { GlobalData } from "../../../Script/Common/GlobalData";
import GameUtil from "../../../Script/Util/GameUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class StoreGuest extends BaseSprite {

    @property({ type: cc.Node, tooltip: "人物" })
    private skes: cc.Node = null;

    private ske: sp.Skeleton = null;

    private data: { isHad: boolean, refresh: Function, pay: Function } = null;

    protected onLoad() {}

    private showSkeleton() {
        this.skes.children.forEach((o) => {
            o.active = false;
        });
        const node = this.skes.children[Math.floor(Math.random() * this.skes.children.length)];
        this.ske = node.getComponent(sp.Skeleton);
        node.active = true;
        node.scaleX = 1;
        GameUtil.playAnimation({ ske: this.ske, animName: 'zoulu', isLoop: true });
    }

    public come(data: { isHad: boolean, refresh: Function, pay: Function }) {
        this.data = data;

        this.showSkeleton();

        const comeX = -GlobalData.stage_width;
        const stopX = GameUtil.getRandomInteger(-80, 200);
        const time = stopX - comeX;
        this.node.x = comeX;
        this.node.y = GameUtil.getRandomInteger(-100, -200);
        this.node.zIndex = 1000 - Math.floor(this.node.y);

        this.node.stopAllActions();
        cc.tween(this.node).sequence(
            cc.moveTo(time / 1000 * 3, cc.v2(stopX, this.node.y)),
            cc.callFunc(() => {
                GameUtil.playAnimation({ ske: this.ske, animName: 'daiji', isLoop: true });
            }),
            cc.delayTime(Math.random() * 3 + 1),
            cc.callFunc(() => {
                GameUtil.playAnimation({ ske: this.ske, animName: 'zoulu', isLoop: true });
                // 刷新货物
                data.refresh && data.refresh();
                data.refresh = null;

                if (data.isHad) {
                    this.pay();
                } else {
                    this.go();
                }
            }),
        ).start();
    }

    private pay() {
        this.node.stopAllActions();
        cc.tween(this.node).sequence(
            cc.moveTo(1.5, cc.v2(this.node.x, -650)),
            cc.callFunc(() => {
                GameUtil.playAnimation({ ske: this.ske, animName: 'daiji', isLoop: true });
            }),
            cc.delayTime(Math.random() * 3 + 1),
            cc.callFunc(() => {
                GameUtil.playAnimation({ ske: this.ske, animName: 'zoulu', isLoop: true });
                this.data.pay && this.data.pay();
                this.data.pay = null;
            }),
            cc.moveTo(1, cc.v2(this.node.x, GameUtil.getRandomInteger(-100, -200))),
            cc.callFunc(()=>{
                this.go();
            }),
        ).start();
    }

    private go() {
        const stopX = -GlobalData.stage_width;
        const time = this.node.x - stopX;

        this.ske.node.scaleX = -1;
        this.node.stopAllActions();
        cc.tween(this.node).sequence(
            cc.moveTo(time / 1000 * 3, cc.v2(stopX, this.node.y)),
            cc.callFunc(() => {
                this.node.destroy();
            }),
        ).start();
    }

    protected update(dt) {
        this.node.zIndex = 2000 - this.node.y;
    }

    private onBtnsClicked(event: cc.Event.EventTouch, eventData: string) {
        const name = event.target.name;
        switch (name) {
            case 'itemCounter':
                {

                }
                break;
        }
    }
}